package com.smfx.yxt.controller.teacher;

import com.alibaba.fastjson.JSONObject;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.model.TeacherWorkSign;
import com.smfx.yxt.reqVo.TeacherWorkSignReqVO;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.service.ITeacherWorkSignService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.DateUtil;
import com.smfx.yxt.utils.HttpClientUtils;
import com.smfx.yxt.utils.JsonUtils;
import com.smfx.yxt.utils.RequestUtil;
import com.smfx.yxt.utils.StringUtil;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping({"common/work"})
@Controller
public class TeacherWorkController extends BaseController
{
  private Logger logger = LoggerFactory.getLogger(TeacherWorkController.class);

  private static String TEACHER_AM_START_DATE = " 6:00";
  private static String TEACHER_AM_END_DATE = " 8:00";

  private static String TEACHER_PM_START_DATE = " 13:50";
  private static String TEACHER_PM_END_DATE = " 15:00";

  @RequestMapping({"cardSign"})
  public String cardSign(Model model) { 
	String openid = this.request.getParameter("openid");
    if (StringUtils.isNotBlank(openid)) {
      TeacherInfo teacher = new TeacherInfo();
      try {
		String result = HttpClientUtils.doGet(Const.BASEPATH + "common/getTeacherInfo?openid="+openid);
		teacher = JsonUtils.jsonToObject(result, TeacherInfo.class);
      } catch (IOException e) {
		e.printStackTrace();
	  }
      this.session.setAttribute("teacherId", teacher.getId());
      this.session.setAttribute("teacherName", teacher.getName());
      return "redirect:/common/work/cardSign";
    }
    model.addAttribute("ip", RequestUtil.getIpAddr(this.request));
    model.addAttribute("teacherName", this.session.getAttribute("teacherName").toString());
    return "work/cardSign"; 
  } 
  
  @RequestMapping({"today"})
  @ResponseBody
  Object todaySign(Integer teacherId) { 
	TeacherWorkSignReqVO vo = new TeacherWorkSignReqVO();
    vo.setTeacherId(Integer.valueOf(this.session.getAttribute("teacherId").toString()));
	try {
		String result = HttpClientUtils.doPost(Const.BASEPATH + "common/todaySign", vo);
		return result;
	} catch (IOException e) {
		e.printStackTrace();
	}
    
    return null; 
  } 
  
  @RequestMapping({"cardSign/submit"})
  @ResponseBody
  Object cardSignSubmit(String ip) { 
	  try { 
		  Integer teacherId = Integer.valueOf(this.session.getAttribute("teacherId").toString());
      Date signTime = new Date();
      Long currTime = Long.valueOf(signTime.getTime());
      String currDate = DateUtil.dateToStr(new Date(), 
        "yyyy-MM-dd");
      Long startDate = null;
      Long endDate = null;
      int signType = 0;
      if (DateUtil.isAmOrPm() == 0) signType = 1; else
        signType = 2;
      if (signType == 1) {
        startDate = 
          Long.valueOf(DateUtil.strToDate(currDate + TEACHER_AM_START_DATE, 
          "yyyy-MM-dd HH:mm").getTime());
        endDate = 
          Long.valueOf(DateUtil.strToDate(currDate + TEACHER_AM_END_DATE, 
          "yyyy-MM-dd HH:mm").getTime());
      } else {
        startDate = 
          Long.valueOf(DateUtil.strToDate(currDate + TEACHER_PM_START_DATE, 
          "yyyy-MM-dd HH:mm").getTime());
        endDate = 
          Long.valueOf(DateUtil.strToDate(currDate + TEACHER_PM_END_DATE, 
          "yyyy-MM-dd HH:mm").getTime());
      }
      if ((currTime.longValue() >= startDate.longValue()) && (currTime.longValue() <= endDate.longValue())) {
        TeacherWorkSignReqVO record = new TeacherWorkSignReqVO();
        record.setTeacherId(teacherId);
        record.setSignType(Integer.valueOf(signType));
        String result = HttpClientUtils.doPost(Const.BASEPATH + "common/todaySignCount", record);
        int count = Integer.valueOf(result);
        if (count > 0) {
          return new RspResult(Status.CARD_SIGN_SUCESS);
        }
        TeacherWorkSign cardSign = new TeacherWorkSign();
        cardSign.setSignType(Integer.valueOf(signType));
        cardSign.setRemark(ip);
        cardSign.setTeacherId(teacherId);
        cardSign.setSignDate(DateUtil.dateToStr(signTime, DateUtil.DEFAULT_DATE_FORMAT));
        result = HttpClientUtils.doPost(Const.BASEPATH + "common/signSubmit", cardSign);
        return JsonUtils.jsonToObject(result, RspResult.class);
      }
      return new RspResult(Status.CARD_SIGN_TIMEOUT);
    } catch (Exception e) {
      this.logger.error("--cardSignSubmit error={}", e);
    }
    return new RspResult(Status.FAIL); 
  }
  
	@RequestMapping("signTotal")
	public String signTotal(Model model) {
		return "work/signTotal";
	}
	
	@RequestMapping("weekTotal")
	@ResponseBody Object weekTotal(Integer diffDay) {
		String teacherId = session.getAttribute("teacherId").toString();
		try {
			String result = HttpClientUtils.doGet(Const.BASEPATH + "common/weekTotal?teacherId="+teacherId+"&diffDay="+diffDay);
			logger.info("----weekTotal result={}", result);
			return result;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

  private String getIp() {
    String ip = null;
    try
    {
      InetAddress inet = null;
      inet = InetAddress.getLocalHost();
      ip = inet.getHostAddress();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    return StringUtil.nullToEmpty(ip);
  }
}